package com.qf.fc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.fc.Param.CommentLikeAdd;
import com.qf.fc.dao.CommentLikeDao;
import com.qf.fc.entity.CommentLike;
import com.qf.fc.entity.MoodLike;
import com.qf.fc.service.CommentLikeService;
import com.qf.fc.vo.R;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * 评论点赞表(CommentLike)表服务实现类
 *
 * @author makejava
 * @since 2024-09-23 23:34:47
 */
@Service("commentLikeService")
@RequiredArgsConstructor
@Slf4j
public class CommentLikeServiceImpl implements CommentLikeService {
    private final CommentLikeDao dao;

    @Override
    public R addLike(CommentLikeAdd add) {
        log.info("新增点赞：{}", add);
        try {
            // 原有的业务逻辑
            if (add == null || add.getCid() == null) {
                return R.fail("亲，请检查参数信息");
            }
            int a = 0;
            LambdaQueryWrapper<CommentLike> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(CommentLike::getUid, add.getUid())
                   .eq(CommentLike::getCid, add.getCid());
            CommentLike commentLike = dao.selectOne(wrapper);
            if (commentLike == null) {
                commentLike = new CommentLike();
                commentLike.setCid(add.getCid());
                commentLike.setUid(add.getUid());
                commentLike.setCreateTime(new Date());
                a = dao.insert(commentLike);
            } else {
                a = dao.deleteById(commentLike.getId());
            }
            if (a > 0) {
                return R.ok("操作成功");
            }
            return R.fail("亲，网络故障，请检查。");
        } catch (Exception e) {
            // 记录异常日志
            log.error("addLike 方法发生异常", e);
            return R.fail("系统异常，请稍后重试");
        }
    }

    @Override
    public R queryMy(Integer uid) {
        return R.ok(dao.selectList(new LambdaQueryWrapper<CommentLike>().eq(CommentLike::getUid,uid)));
    }
}

